package cn.yh.nft.turbo.order.validator;

import cn.yh.nft.turbo.api.goods.model.BaseGoodsInventoryVO;
import cn.yh.nft.turbo.api.inventory.request.InventoryRequest;
import cn.yh.nft.turbo.api.inventory.service.InventoryFacadeService;
import cn.yh.nft.turbo.api.order.request.OrderCreateRequest;
import cn.yh.nft.turbo.api.order.service.InventoryWrapperService;
import cn.yh.nft.turbo.base.response.SingleResponse;
import cn.yh.nft.turbo.order.OrderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static cn.yh.nft.turbo.api.order.constant.OrderErrorCode.INVENTORY_NOT_ENOUGH;

/**
 * @ClassName: StockValidator
 * @Author: 草莓爱上啵啵
 *
 *      库存效验器
 */
public class StockValidator extends BaseOrderCreateValidator {

    private InventoryFacadeService inventoryFacadeService;

    @Override
    protected void doValidate(OrderCreateRequest request) throws OrderException {
        InventoryRequest inventoryRequest = new InventoryRequest();
        inventoryRequest.setGoodsId(request.getGoodsId());
        inventoryRequest.setGoodsType(request.getGoodsType());
        inventoryRequest.setIdentifier(request.getIdentifier());
        inventoryRequest.setInventory(request.getItemCount());

        SingleResponse<Integer> response = inventoryFacadeService.queryInventory(inventoryRequest);

        if (!response.getSuccess()) {
            throw new OrderException(INVENTORY_NOT_ENOUGH);
        }

        Integer inventory = response.getData();

        if (inventory == 0) {
            throw new OrderException(INVENTORY_NOT_ENOUGH);
        }

        if (inventory < request.getItemCount()) {
            throw new OrderException(INVENTORY_NOT_ENOUGH);
        }

    }

    public StockValidator(InventoryFacadeService inventoryFacadeService) {
        this.inventoryFacadeService = inventoryFacadeService;
    }

    public StockValidator() {
    }
}
